{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 异常\n",
    "异常是程序运行运行时引发的错误。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. 异常例子"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "ZeroDivisionError",
     "evalue": "division by zero",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mZeroDivisionError\u001b[0m                         Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[1], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m n \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m3\u001b[39m\u001b[38;5;241m/\u001b[39m\u001b[38;5;241m0\u001b[39m\n",
      "\u001b[0;31mZeroDivisionError\u001b[0m: division by zero"
     ]
    }
   ],
   "source": [
    "n = 3/0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "ZeroDivisionError: division by zero\n",
    "ZeroDivisionError   错误类别:除零错误\n",
    "division by zero    说明:使用0作为除数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'abc' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[2], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m n \u001b[38;5;241m=\u001b[39m \u001b[38;5;28meval\u001b[39m(\u001b[38;5;28minput\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m请输入一个数字:\u001b[39m\u001b[38;5;124m\"\u001b[39m))\n\u001b[1;32m      2\u001b[0m \u001b[38;5;28mprint\u001b[39m(n)\n\u001b[1;32m      3\u001b[0m \u001b[38;5;28mprint\u001b[39m(n\u001b[38;5;241m+\u001b[39m\u001b[38;5;241m1\u001b[39m)\n",
      "File \u001b[0;32m<string>:1\u001b[0m\n",
      "\u001b[0;31mNameError\u001b[0m: name 'abc' is not defined"
     ]
    }
   ],
   "source": [
    "n = eval(input(\"请输入一个数字:\"))\n",
    "print(n)\n",
    "print(n+1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "NameError: name 'abc' is not defined\n",
    "NameError: 名字错误\n",
    "名字'abc'没有定义"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. 异常的处理\n",
    "    先尝试运行代码，如果没有问题就正常执行；如果发生了错误就尝试着去捕获和处理；上述均无法处理，程序才崩溃。\n",
    "\n",
    "格式:\n",
    "```python\n",
    "try ... except ...\n",
    "try ... except ... except ...\n",
    "try ... except ... else ...\n",
    "try ... except ... finally ...\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "基本语法如下:\n",
    "```\n",
    "try:\n",
    "    语句块\n",
    "except:\n",
    "    异常处理语句块\n",
    "else:\n",
    "    没有异常时执行语句块\n",
    "finally:\n",
    "    无论是否有异常都会执行的语句块\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.1 例子1:处理除数为0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "除数不能为0\n"
     ]
    }
   ],
   "source": [
    "x=eval(input())\n",
    "try:\n",
    "    n=1.0/x\n",
    "    print(n)\n",
    "except:\n",
    "    print(\"除数不能为0\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.2 例子2: 处理输入错误"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "输入错误，请输入数字!\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    n=eval(input(\"请输入数字n:\"))\n",
    "    y=n**2\n",
    "    print(y)\n",
    "except:\n",
    "    print(\"输入错误，请输入数字!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.3 例子3:try-except-else"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "输入错误，请输入数字!\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    n=eval(input(\"请输入数字n:\"))\n",
    "except:\n",
    "    print(\"输入错误，请输入数字!\")\n",
    "else:\n",
    "    y=n**2\n",
    "    print(y)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.4 例子4: try-except-else\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "除数不能为0\n"
     ]
    }
   ],
   "source": [
    "x=eval(input())\n",
    "try:\n",
    "    n=1.0/x\n",
    "except:\n",
    "    print(\"除数不能为0\")\n",
    "else:\n",
    "    print(n)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.5 例子5:try-except-finally"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "除数不能为0\n",
      "这是一则除法运算题!\n"
     ]
    }
   ],
   "source": [
    "x=eval(input())\n",
    "try:\n",
    "    n=1.0/x\n",
    "    print(n)\n",
    "except:\n",
    "    print(\"除数不能为0\")\n",
    "finally:\n",
    "    print(\"这是一则除法运算题!\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
